#include "DHT.h"
#define DHTPIN 14

// Uncomment whatever type you're using!
//#define DHTTYPE DHT11 // DHT 11
#define DHTTYPE DHT22 // DHT 22 (AM2302)
//#define DHTTYPE DHT21 // DHT 21 (AM2301)

const int pump = 12; 
const int light = 11;
const int heater = 10;
const int fan = 9; 
int val = 0;
int incomingByte;
DHT dht(DHTPIN, DHTTYPE);

void setup() {
   
  Serial.begin(9600);
  delay(25);
  dht.begin();
  pinMode(fan, OUTPUT);
  pinMode(heater, OUTPUT);
  pinMode(light, OUTPUT);
  pinMode(pump, OUTPUT);
  digitalWrite(fan, HIGH);
  digitalWrite(heater, HIGH);
  digitalWrite(light, HIGH);
  digitalWrite(pump, HIGH);
//  Serial.println("Humidity-H Temperature-T");
//  Serial.println("Relay   on   off   Read   Pin");
//  Serial.println("fan     a     1     q     9");
//  Serial.println("heater  s     2     w     10");
//  Serial.println("light   d     3     e     11");
//  Serial.println("pump    f     4     r     12");
//  Serial.println("");
//  Serial.println("Sensor     pin");
//  Serial.println("Temp&Hum.  14");
//  Serial.println("Sensor     15");
//  Serial.println("Sensor     16");
//  Serial.println("Sensor     17");
  delay(0);
}



void loop(){

  if (Serial.available() > 0) {
   incomingByte = Serial.read(); 
   
    if (incomingByte == 'H') {
  
       float h = dht.readHumidity();
       float t = dht.readTemperature();

       if (isnan(t) || isnan(h)) {
       Serial.println(999);
       }    
       else {
       Serial.println(h);
       }
    }

    if (incomingByte == 'T') {
  
       float h = dht.readHumidity();
       float t = dht.readTemperature();
    
       if (isnan(t) || isnan(h)) {
       Serial.println(999);
       }    
       else {
       Serial.println(t);
       }
    }
   
    if (incomingByte == 'q') { 
       val = digitalRead(fan);
       if(val == LOW){
       Serial.println("On");
       }
    else{ 
       Serial.println("Off"); 
      }
    } 
    if (incomingByte == 'w') { 
       val = digitalRead(heater);
       if(val == LOW){
       Serial.println("On");
       }
    else{ 
       Serial.println("Off"); 
       }
    }
    if (incomingByte == 'e') { 
       val = digitalRead(light);
       if(val == LOW){
       Serial.println("On");
       }
    else{ 
       Serial.println("Off"); 
       }
    }
    if (incomingByte == 'r') { 
       val = digitalRead(pump);
       if(val == LOW){
       Serial.println("On");
       }
    else{ 
       Serial.println("Off"); 
       }
    }
    if (incomingByte == 'f') {
      digitalWrite(fan, LOW);
      delay(0);
      }
    if (incomingByte == '1') {
      digitalWrite(fan, HIGH);
      delay(0);
      }
    
    if (incomingByte == 'h') {
      digitalWrite(heater, LOW);
      delay(0);
      }
        if (incomingByte == '2') {
      digitalWrite(heater, HIGH);
      delay(0);
      }
        if (incomingByte == 'l') {
      digitalWrite(light, LOW);
      delay(0);
      }
    if (incomingByte == '3') {
      digitalWrite(light, HIGH);
      delay(0);
      } 
    if (incomingByte == 'p') {
      digitalWrite(pump, LOW);
      delay(0);
      }
        if (incomingByte == '4') {
      digitalWrite(pump, HIGH);
      }
  }
}

